import os
import pickle
import numpy as np
from tqdm import tqdm
from PIL import Image
from encoder.color import encode_color

data_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data'))
image_path = os.path.join(data_path, 'raw_images')

print(data_path)

file_paths = []
for root, dirs, files in os.walk(image_path):
    for name in files:
        if name.endswith('JPEG'):
            file_path = os.path.join(root, name)
            file_paths.append(file_path)

cache_datas = []
for id in tqdm(range(len(file_paths))):
    file_path = file_paths[id]
    image = Image.open(file_path)
    
    image_feature = encode_color(image)
    data = {
        'id' : id,
        'path' : file_path,
        'features' : image_feature
    }
    cache_datas.append(data)

with open(os.path.join(data_path, f'cache_color_feature.bin'), 'wb') as fp:
    pickle.dump(cache_datas, fp)